package loginTest;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ShowLogin extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();
        if (cookies != null){
            for (int i = 0; i <cookies.length ; i++) {
                if ("username".equals(cookies[i].getName())) {
                    req.setAttribute("name", cookies[i].getValue());
                    req.getRequestDispatcher("loginok").forward(req, resp);
                }
            }
        }
        resp.setContentType("text/html;charset=utf-8");

        String errorMsg="";
        String errorps="";
        if (cookies != null){
            for (int i = 0; i <cookies.length ; i++) {
                if ("pass".equals(cookies[i].getName())){
                    cookies[i].setMaxAge(0);
                    errorps="密码错误";
                }
            }
        }
        if (cookies != null){
            for (int i = 0; i <cookies.length ; i++) {
                if ("name".equals(cookies[i].getName())){
                    cookies[i].setMaxAge(0);
                    errorMsg="用户名错误";
                }
            }
        }
        String str="     <form action=\"/login/c\" method=\"post\">\n" +
                "        用户名:<input type=\"text\" name=\"username\"/> <span>"+errorMsg+"</span<br />\n</br>" +
                "        &nbsp&nbsp&nbsp密码:<input type=text name=password /> <span>"+errorps+"</span>" +
                "        </br>爱好：<input type=\"checkbox\" name=\"hobby\" value=\"swim\"/>游泳\n" +
                "        <input type=\"checkbox\" name=\"hobby\" value=\"sleep\"/>睡觉\n" +
                "        <input type=\"checkbox\" name=\"hobby\" value=\"basketball\"/>篮球\n" +
                "        <br />\n" +
                "        <input type=\"submit\" value=\"提交\"/>\n" +
                "        </form>";
        resp.getWriter().write(str);

    }
}
